package com.hqyj.finance.server.bill.financebill.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.finance.server.bill.financebill.client.UserClient;
import com.hqyj.finance.server.bill.financebill.entity.UserExpenses;
import com.hqyj.finance.server.bill.financebill.json.R;
import com.hqyj.finance.server.bill.financebill.service.UserExpensesService;
import com.hqyj.finance.server.bill.financebill.views.UserExpenseCatalogMonthsCharts;
import com.hqyj.finance.server.bill.financebill.views.UserExpenseMonthsCharts;
import com.hqyj.finance.server.bill.financebill.views.UserExpenseMonthsView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("expense")
@RestController
public class UserExpensesController {

    @Resource
    private UserExpensesService userExpensesService;

    @Resource
    private UserClient userClient;

    @PostMapping("/")
    public R saveUserExpenseInfo(@RequestBody UserExpenses userExpenses) {
        try {
            if (userExpensesService.saveUserExpensesInfo(userExpenses)) {
                return R.success("新增用户支出信息成功！");
            }
            return R.fail("新增用户支出信息失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }


    @GetMapping("queryUserInfo")
    public R queryUserInfoByUid(@RequestParam("uid") Integer uid) {
        try {
            return userClient.queryUserInfoByUid(uid);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    //http.get('api/bills/expense',params)
    //{uid:xxxx,currentPageNum:10}
    //api/bills/expense/24/1
    @GetMapping("/pager")
    public R queryUserExpensePagerByUid(@RequestParam("uid") Integer uid, @RequestParam("currentPageNum") Integer currentPageNum) {
        try {
            Page<UserExpenses> page = userExpensesService.queryUserExpensePagerByUid(uid, currentPageNum);

            return R.success("查询用户支出分页数据成功！", page);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("/{expensesId}")
    public R queryUserExpenseInfoByExpensesId(@PathVariable("expensesId") Integer expensesId) {
        try {
            UserExpenses userExpenses = userExpensesService.queryUserExpenseInfoByExpensesId(expensesId);
            return R.success("查询用户支出详情资料成功！", userExpenses);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("search")
    public R searchUserMonthExpensePager(@RequestParam("uid") Integer uid, @RequestParam("realName") String realName, @RequestParam("month") String month, @RequestParam("currentPageNum") Integer currentPageNum) {
        try {
            UserExpenseMonthsView userExpenseMonthsView = userExpensesService.queryUserExpenseMonthsViewByUid(uid, realName, month, currentPageNum);
            return R.success("查询用户指定月份的支出详情成功！", userExpenseMonthsView);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("export")
    public R exportUserMonthExpenseToExcelFile(@RequestParam("uid") Integer uid, @RequestParam("realName") String realName, @RequestParam("month") String month, @RequestParam("currentPageNum") Integer currentPageNum) {
        try {
            UserExpenseMonthsView userExpenseMonthsView = userExpensesService.queryUserExpenseMonthsViewByUid(uid, realName, month, currentPageNum);
            String uploadSuccessFileUrl = userExpensesService.exportUserExpenseMonthViewAsExcel(userExpenseMonthsView);
            return R.success("导出用户支出明细excel文件成功！", uploadSuccessFileUrl);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @GetMapping("echarts/{type}")
    public R getUserExpenseEchartsByType(@PathVariable("type") String type, @RequestParam("uid") Integer uid, @RequestParam("realName") String realName, @RequestParam("month") String month) {
        try {
            switch (type) {
                case "curve":
                    UserExpenseMonthsCharts userExpenseMonthsCharts = userExpensesService.queryUserExpenseMonthsChartsByUid(uid, realName, month);
                    return R.success("查询支出曲线图信息成功！", userExpenseMonthsCharts);
                //break;
                case "pie":
                    UserExpenseCatalogMonthsCharts userExpenseCatalogMonthsCharts = userExpensesService.queryUserExpenseCatalogMonthsCharts(uid, realName, month);
                    return R.success("查询支出饼状图信息成功！", userExpenseCatalogMonthsCharts);
                //break;
                default:
                    return R.success("查询Echarts图表信息成功！",null);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

}
